package gggoal

import (
	"github.com/shopspring/decimal"
	"wzgames/internal/constant"
	"wzgames/internal/model/gkindp"
	"wzgames/internal/secure/rng"
	"wzgames/pkg/utils/calcutil"
)

type Gift struct {
}

func (gf *Gift) Init(cfg *gkindp.GoalConfig) {

}

func (gf *Gift) Update(cfg *gkindp.GoalConfig) {

}

func (gf *Gift) Calculate(servSeed, clientSeed, betType string) []int32 {
	var limit int
	var rows int32
	switch betType {
	case constant.KeySm:
		limit = 4
		rows = 3
	case constant.KeyMd:
		limit = 7
		rows = 4
	case constant.KeyLg:
		limit = 10
		rows = 5
	}
	var mines []int32
	nums := rng.CalculateHash(servSeed, clientSeed, rng.ChunkSize, limit)
	for i := 0; i < limit; i++ {
		idx := decimal.NewFromFloat(nums[i]).Mul(decimal.NewFromInt32(rows))
		mines = append(mines, int32(idx.IntPart()))
	}
	return mines
}

func (gf *Gift) GetGoalRowCol(config *gkindp.GoalConfig, tp string) *gkindp.GoalRowCol {
	switch tp {
	case constant.KeySm:
		return config.GameConfig.Sm
	case constant.KeyMd:
		return config.GameConfig.Md
	case constant.KeyLg:
		return config.GameConfig.Lg
	}

	return nil
}

func (gf *Gift) CalcMultiplier(config *gkindp.GoalConfig, digs int, tp string) float64 {
	if digs <= 0 {
		return 0
	}
	var coEff []float64
	switch tp {
	case constant.KeySm:
		coEff = config.GameConfig.CoeffSm
	case constant.KeyMd:
		coEff = config.GameConfig.CoeffMd
	case constant.KeyLg:
		coEff = config.GameConfig.CoeffLg
	}
	l := len(coEff)
	if digs > l {
		digs = l
	}

	return coEff[digs-1]
}

func (gf *Gift) CalcMultiplier1(config *gkindp.GoalConfig, digs int32, tp string) decimal.Decimal {
	goalRC := gf.GetGoalRowCol(config, tp)
	if digs > goalRC.Col {
		digs = goalRC.Col
	}
	totalCells := goalRC.Row * goalRC.Col
	u := decimal.NewFromFloat(100.0 - config.HouseEdge).Div(calcutil.BaseCoefficient)
	mines := decimal.NewFromInt32(goalRC.Col)
	for v := int32(0); v < digs; v++ {
		x := decimal.NewFromInt32(totalCells - v*goalRC.Row)
		u = u.Mul(x).Div(x.Sub(mines.Sub(decimal.NewFromInt32(v))))
	}
	return u
}
